package ph.com.gs3.formalistics.service.services;

import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.valueobjects.business.User;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;

public class FullMessagingDataUpdateService extends Service {

	public static final String TAG = FullMessagingDataUpdateService.class.getSimpleName();

	public static final String EXTRA_CURRENT_USER = "current_user";
	private User currentUser;

	// ============================================================================
	// {{ Service Life Cycle Methods

	@Override
	public int onStartCommand(final Intent intent, int flags, int startId) {

		initialize(intent);

		registerBroadcastReceivers();
		startGetOnlineUsersService();

		FLLogger.d(TAG, "service done");

		return START_REDELIVER_INTENT;

	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		FLLogger.d(TAG, "Service destroyed");

		// dismiss notification
		// ServiceEventNotifierUtility.getDefaultInstance().dismissPrimaryNotification(this);

		unregisterBroadcastReceivers();

	}

	// }}

	private void initialize(Intent intent) {

		Bundle extras = intent.getExtras();
		currentUser = (User) extras.getSerializable(EXTRA_CURRENT_USER);

	}

	// =================================================================================
	// {{ Service Management Methods

	private void startGetOnlineUsersService() {

		Intent intent = new Intent(FullMessagingDataUpdateService.this, GetOnlineUsersService.class);
		intent.putExtra(GetOnlineUsersService.EXTRA_CURRENT_USER, currentUser);
		startService(intent);

	}

	private void startFetchMessagesService() {

		Intent intent = new Intent(FullMessagingDataUpdateService.this, FetchMessagesService.class);
		intent.putExtra(GetOnlineUsersService.EXTRA_CURRENT_USER, currentUser);
		startService(intent);

	}

	private void registerBroadcastReceivers() {

		// online users

		IntentFilter onlineUsersIntentFilter = new IntentFilter();

		onlineUsersIntentFilter.addAction(GetOnlineUsersService.ACTION_ON_ERROR);
		onlineUsersIntentFilter.addAction(GetOnlineUsersService.ACTION_ON_ONLINE_USERS_UDPATED);
		onlineUsersIntentFilter.addCategory(Intent.CATEGORY_DEFAULT);

		registerReceiver(getOnlineUsersBroadcastReceiver, onlineUsersIntentFilter);

		// fetch messages

		IntentFilter fetchMessagesIntentFilter = new IntentFilter();

		fetchMessagesIntentFilter.addAction(FetchMessagesService.ACTION_ON_THREADS_FETCHED);
		fetchMessagesIntentFilter
		        .addAction(FetchMessagesService.ACTION_ON_MESSAGES_FOR_THREAD_FETCHED);
		fetchMessagesIntentFilter.addAction(FetchMessagesService.ACTION_ON_ALL_MESSAGES_FETCHED);
		fetchMessagesIntentFilter.addCategory(Intent.CATEGORY_DEFAULT);
		
		registerReceiver(fetchMessagesBroadcastReceiver, fetchMessagesIntentFilter);

	}

	private void unregisterBroadcastReceivers() {
		unregisterReceiver(getOnlineUsersBroadcastReceiver);
	}

	// }}

	// ============================================================================
	// {{ Broadcast Receivers

	private BroadcastReceiver getOnlineUsersBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {

			FLLogger.d(TAG, "Done getting online users");			
			startFetchMessagesService();

		}

	};

	private BroadcastReceiver fetchMessagesBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {

			String action = intent.getAction();

			FLLogger.d(TAG, "Triggered: " + action);

		}

	};

	// }}
}
